<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<%@ taglib prefix="product" tagdir="/WEB-INF/tags/product"%>
<%@ taglib prefix="system" tagdir="/WEB-INF/tags/system"%>
<%@ taglib prefix="sales" tagdir="/WEB-INF/tags/sales"%>
<title><fmt:message key="shoppingcart.shoppingCart" /></title>
<div class="main">
	<div class="shopping_step">
		<ul>
			<li class="s1"><span class="curr"><a href="#">1.<fmt:message key="checkout.process.step1" /></a></span></li>
			<li class="s2"><span>2.<fmt:message key="checkout.process.step2" /></span></li>
			<li class="s3"><span>3.<fmt:message key="checkout.process.step3" /></span></li>
		</ul>
	</div>
	<sales:exceptionMsg msgContent="${exceptionMsg }"></sales:exceptionMsg>
	<div class="cart">
		<div class="top"><span><fmt:message key="shoppingcart.myShoppingcart" /></span></div>
		<div class="content">
			<form id="shoppingcartForm" name="shoppingcartForm">
				<div id="shoppingcartItemsDiv">
					<%@ include file="include/shoppingcartItems.jsp"%>
				</div>
			</form>
			<div id="shippingcartBillDiv">
				<%@ include file="include/shoppingcartBill.jsp"%>
			</div>
		</div>
	</div>
	
	<div class="wrap_03">
		<div class="tab_03">
			<ul class="content clearfix" id="rpBoxesUL">
				<li id="1" class="curr"><a href="javascript:void(0)"><fmt:message key="promotion.guessLove"/></a></li>
				<li id="2"><a href="${ctxPath}/customer/csFavorites.html" title="<fmt:message key="shoppingcart.searchMoreFavorite" />"><fmt:message key="shoppingcart.myFavorite" /></a></li>
			</ul>
		</div>
		<div class="product_arrange_04" id="rpBox1">
			<sales:recommendedProductList recommendedTypeName="also_buy" sourceKind="1" maxResults="10" sourceIds="${sourceIds}" 
				returnProductListName="alsoBuyProductList" returnRecommendedTypeName="alsoBuyType" isNeedRand="true">
				<%@ include file="../sales/recommendedProduct/display/shoppingcartAlsoBuy.jspf"%>
			</sales:recommendedProductList>	
		</div>
		<div class="product_arrange_04" id="rpBox2" style="display:none;">
			<%@ include file="include/shoppingcartFavorite.jsp"%>
		</div>
	</div>
	
</div>
<c:set var="probizJs_shoppingcart" value="true" scope="request"/>
<script type="text/javascript">
$j(document).ready(function () {
	$j("#minicartDiv").remove();	//禁止迷你购物车弹出
	<%--start:购买数量事件--%>
	var firstQuantity = $j('input[name="quantitys"]:first');
	if(firstQuantity != null){
		firstQuantity.focus();
	}	
	$j('input[name="quantitys"]').each(function(){
		$j(this).bind('keydown',quantityOnEnter);
	});
	//确认购物事件
	function quantityOnEnter(event){
		var jPrev = $j(this).parents('tr:first').prev().find('input[name="quantitys"]');
		var jNext = $j(this).parents('tr:first').next().find('input[name="quantitys"]');
		switch(event.keyCode){
			case 13: 
				//回车
				if(jNext.length != 0){
					//有下个，则焦点下移
					$j(this).blur();
					jNext.focus().select();
				} 
				//屏蔽回车提交表单事件
				return false;
			case 40	:
				//下
				if(jNext.length != 0){
					$j(this).blur();
					jNext.focus();
				}	
				break;
			case 38	:
				//上
				if(jPrev.length != 0){
					$j(this).blur();
					jPrev.focus();
				}	
				break;	
		}
	}	
	<%--end:购买数量事件--%>
	
	$j('#rpBoxesUL li').each(function(i,o){
		$j(o).bind("mouseover",function(){
			$j('#rpBoxesUL li').each(function(i,o){
				$j(this).removeClass("curr");
				$j('#rpBox'+this.id).hide();
			});
			$j(this).addClass("curr");
			$j('#rpBox'+this.id).show();
			
		});
	
	});
});
</script>

<script type="text/javascript">

//结帐按钮
function toCheckout() {
	//1.读取页面上的采购数量
	var productSkuCodes = new Array();
	var quantitys = new Array();
	var numberFormatExceptionOccur = false;
	var errorElement;
	$j("[name=productSkuCodes]").each(function (i) {
		productSkuCodes.push(this.value);
	});
	$j("[name=quantitys]").each(function (i) {
		if (isNaN(this.value)) {
			errorElement = this;
			numberFormatExceptionOccur = true;
		} 
		quantitys.push(this.value);
	});
	
	//2.验证用户输入的采购数量
	if (numberFormatExceptionOccur) {
		alert("<fmt:message key="shoppingcartItem.quantityNotNull" />");
		$j(errorElement).focus();
		$j(errorElement).select();
		return;
	}
	
	//3.保存采购数量进购物车
	$j("#btnCheckout").hide();	//隐藏按钮
	$j("#btnContinue").hide();
	$j(".loading").show();
	changeQuantitys(productSkuCodes, quantitys, 
		function() {
			//4.跳转到结帐页面
			window.location.href='${ctxPath}/checkout.html';
		},
		function() {
			//失败回调函数--把按钮恢复显示
			$j(".loading").hide();
			$j("#btnCheckout").show();
			$j("#btnContinue").show();
		}
	);
}

//删除商品项
function deleteItem(productSkuCode, obj) {
	if(confirm(__FMT.shoppingcart_isDeleted)){
		showWaitMessage("loading", obj);
		delProductItem(productSkuCode, function() {
			$j(obj).parents("tr:first").remove();
			refreshBill();
		});
	}
}

//购买数量减1
function quantityMinus(productSkuCode, tabindex) {
	var filter = "input[name=quantitys][tabindex=" + tabindex + "]";
	var quantityElement = $j(filter);
	if(quantityElement.val()!="" && quantityElement.val()>1)
	var quantity = parseInt(quantityElement.val()) - 1;
	quantityElement.val(quantity);
	quantityOnChange(productSkuCode, quantity);
}

//购买数量加1
function quantityPlus(productSkuCode, tabindex) {
	var filter = "input[name=quantitys][tabindex=" + tabindex + "]";
	var quantityElement = $j(filter);
	if(quantityElement.val()!="")
	var quantity = parseInt(quantityElement.val()) + 1;
	quantityElement.val(quantity);
	quantityOnChange(productSkuCode, quantity);
}

//更新商品项数量输入框触发事件
function quantityOnChange(productSkuCode, quantity) {
	changeQuantity(productSkuCode, quantity, afterQuantityChanged);		//定义在cart.js
}

//更新数量后的回调函数
function afterQuantityChanged(json) {
	var filter;
	
	//1. 更新商品项小计
	filter = "#totalSpan" + json.hashCode;
	$j(filter).html(json.totalShow);
	
	//2. 更新商品项节省金额小计
	filter = "#discountSpan" + json.hashCode;
	var discount = parseInt(json.itemDiscountAmount);
	if (discount > 0) {
		$j(filter).html("(<fmt:message key="minicart.money.shen" />" + json.itemDiscountAmountShow + ")");
	} else {
		$j(filter).html("");
	}
	//3. 更新商品项价格
	filter = "#priceSpan" + json.hashCode;
	$j(filter).html(json.priceShow);
	
	//4. 刷新购物车帐单信息
	refreshBill();
}


//刷新购物车帐单信息
function refreshBill() {
	$j("#shippingcartBillDiv").load("${ctxPath}/cart/shoppingcart.html?doAction=refreshBill&decorator=blank");
}

//刷新商品项信息
function refreshItems() {
	$j("#shoppingcartItemsDiv").load("${ctxPath}/cart/shoppingcart.html?doAction=refreshItems&decorator=blank");
}

//商品加入购物车后的回调函数
function addToCartCallback(json,sourceEvent) {
	jAlert("<fmt:message key="shoppingcart.addToCartSuccess"/>");
	refreshItems();
	refreshBill();
}

//商品添加到收藏夹后的回调函数
function addFavoriteProductCallback(json,sourceEvent){
	promptFavoriteMessage(json, sourceEvent);
	$j("#rpBox2").load("${ctxPath}/cart/shoppingcart.html?doAction=refreshShoppingcartFavorite&decorator=blank");
	
}
//删除收藏夹商品的回调函数
function deleteFavoriteCallback(){
	if($j('#favoriteListDiv li').length==0){
		html = '<div class="msg"><fmt:message key="shoppingcart.favorite.noItems"/></div>';
		$j("#rpBox2").html(html);
	}
				
		
}

//显示等待信息，例如“正在读取XXX信息，请稍等”
showWaitMessage = function(info, obj) {
	try{
		if (obj == null) return;
		clearWaitMessage();
		$j(obj).hide();
		$j("<i class='loading'>" + info + "</i>").insertAfter(obj);
	}catch(e){ alert(e); }
} 
	
//清除等待信息
clearWaitMessage = function(obj) {
	try {
		$j(".loading").remove();
		if (obj != null) $j(obj).show();
	} catch(e){ alert(e); }
}

//具体促销显示
function getPromoValue(event){
  $importDwr(function() {
	shoppingcartDwr.getPromoValue(function(jsonText) {
	    var str="";
		var json = eval("(" + jsonText + ")");
		for(var i=0; i< json.length; i++){
			str+="<li><h3>"+json[i].messages1+"</h3></li>";
		};
		$j("#proms").html(str);
	})
  })
};

//判断输入是否是数字
isNumber = function (e) {
	if ($j.browser.msie) {
		if ( ((event.keyCode > 47) && (event.keyCode < 58)) ||  (event.keyCode == 8) ) {
			return true;
		} else {
			return false;
		}
	} else {
		if ( ((e.which > 47) && (e.which < 58)) ||  (e.which == 8) ) {
			return true;
		} else {
			return false;
		}
	}
};

</script>
